/*
 * TransiCast City Visions
 * (c) 2014 Joachim Pfeiffer
 * www.transicast.com
 */
package com.transicast.cityvisions;

import com.metaio.sdk.jni.Vector2d;

public class ExponentialSmoother {

	private float						mAlpha;
	private int							mExponent;

	private float[] last = new float[2];
	private float[] current = new float[2];

	public Vector2d pushValues(Vector2d coords) {
		float[] values = new float[2];
		values[0] = coords.getX();
		values[1] = coords.getY();

	    for (int i = 0; i < values.length; i++) {
	    	last[i] = current[i];
	        float diff = values[i] - last[i];
	        float correction = diff * mAlpha;
	        for (int j = 1; j < mExponent; ++j) {
	          correction *= Math.abs(diff);
	        }
	        if (correction > Math.abs(diff) ||
	            correction < -Math.abs(diff)) correction = diff;
	        current[i] = last[i] + correction;
		}
        return new Vector2d(current[0], current[1]);
	}

	public ExponentialSmoother(float alpha, int exponent) {
	    mAlpha = alpha;
	    mExponent = exponent;
	}
}
